//
//  XY.m
//  ZhaoQiPei
//
//  Created by 找汽配 on 2017/5/6.
//  Copyright © 2017年 XiangYun. All rights reserved.
//

#import "JTWrapNavigationController+XY.h"
#import "JTNavigationController.h"
#import "UIViewController+JTNavigationExtension.h"
#import "XYViewController.h"

@implementation JTWrapNavigationController (XY)

- (void)pushToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    JTNavigationController * jt_navigationController = self.navigationController;
    NSMutableArray * mArray = [jt_navigationController.viewControllers mutableCopy];
    [mArray removeLastObject];
    
    viewController.jt_navigationController = jt_navigationController;
    if (mArray.count > 0) {
        viewController.jt_fullScreenPopGestureEnabled = jt_navigationController.fullScreenPopGestureEnabled;
        UIImage * backButtonImage = jt_navigationController.backButtonImage;
        if (!backButtonImage) {
            backButtonImage = [UIImage imageNamed:kDefaultBackImageName];
        }
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUpInside)];
    }
    [mArray addObject:[JTWrapViewController wrapViewControllerWithViewController:viewController]];
    [self.navigationController setViewControllers:mArray animated:animated];
}

- (void)pushViewController:(UIViewController *)viewController withoutClasses:(NSArray<Class>*)classes animated:(BOOL)animated {
    JTNavigationController * jt_navigationController = self.navigationController;
    NSMutableArray * mArray = [jt_navigationController.viewControllers mutableCopy];
    for (NSInteger i = mArray.count - 1; i >= 0; --i) {
        UIViewController * controller = jt_navigationController.jt_viewControllers[i];
        if ([classes containsObject:[controller class]]) {
            [mArray removeObjectAtIndex:i];
        }
        else {
            break;
        }
    }
    
    viewController.jt_navigationController = jt_navigationController;
    if (mArray.count > 0) {
        viewController.jt_fullScreenPopGestureEnabled = jt_navigationController.fullScreenPopGestureEnabled;
        UIImage * backButtonImage = jt_navigationController.backButtonImage;
        if (!backButtonImage) {
            backButtonImage = [UIImage imageNamed:kDefaultBackImageName];
        }
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUpInside)];
    }
    [mArray addObject:[JTWrapViewController wrapViewControllerWithViewController:viewController]];
    [self.navigationController setViewControllers:mArray animated:animated];
}

- (void)popViewControllerWithoutClasses:(NSArray<Class>*)classes animated:(BOOL)animated {
    JTNavigationController * jt_navigationController = self.navigationController;
    UIViewController * lastViewController = nil;
    for (NSInteger i = jt_navigationController.viewControllers.count - 1; i >= 0; --i) {
        UIViewController * controller = jt_navigationController.jt_viewControllers[i];
        if (![classes containsObject:[controller class]]) {
            lastViewController = jt_navigationController.viewControllers[i];
            break;
        }
    }
    [self.navigationController popToViewController:lastViewController animated:animated];
}

@end
